/**
 * @description: 获取最近几天整点的时间戳
 * @param {number} days
 */
export const getLastAnyDaysHourlyTimestamps = function (days: number) {
  if (!days) return;
  const timestamps = [];
  const now = new Date();
  for (let i = 0; i < days; i++) {
    const day = new Date(now.getTime() - i * 24 * 60 * 60 * 1000);
    for (let hour = 0; hour < 24; hour++) {
      const date = new Date(
        day.getFullYear(),
        day.getMonth(),
        day.getDate(),
        hour,
        0,
        0,
        0
      );
      timestamps.push(date.getTime());
    }
  }
  return timestamps;
};

/**
 * @description: 传入开始个结束时间,根据颗粒度类型返回,点的时间戳数组
 * @param {1} type 颗粒度 x 小时
 * @param {0} start 开始 时间戳 （毫秒级）
 * @param {0} end 结束 时间戳
 * @return {*} 点的时间戳数组
 * @author: zhj1214
 */
export const getTimestampToType = (
  startTimestamp: 0,
  endTimestamp: 0,
  hours = 1
) => {
  let intervalHours = hours || 1;
  const result = [];
  let currentDate = new Date(startTimestamp);
  const endDate = new Date(endTimestamp);

  // 调整当前时间到最近的指定间隔时间点
  const remainder =
    currentDate.getMinutes() * 60 +
    currentDate.getSeconds() +
    currentDate.getMilliseconds() / 1000;
  if (remainder !== 0) {
    currentDate = new Date(
      currentDate.getFullYear(),
      currentDate.getMonth(),
      currentDate.getDate(),
      currentDate.getHours() + 1,
      0,
      0,
      0
    );
  }

  while (currentDate <= endDate) {
    result.push(currentDate.getTime());
    currentDate = new Date(
      currentDate.getFullYear(),
      currentDate.getMonth(),
      currentDate.getDate(),
      currentDate.getHours() + intervalHours,
      0,
      0,
      0
    );
  }

  return result;
};

/**
 * @description: 获取最近 x 个月,每天 23 点 59 分 59 秒的时间戳
 * @param {number} months
 */
export const getAnyMonthDailyTimestamps = function (months: number) {
  const timestamps = [];
  const now = new Date();
  const oneMonthAgo = new Date(
    now.getTime() - months * 30 * 24 * 60 * 60 * 1000
  );
  let currentDate = new Date(oneMonthAgo);
  while (currentDate <= now) {
    const date = new Date(
      currentDate.getFullYear(),
      currentDate.getMonth(),
      currentDate.getDate(),
      23,
      59,
      59,
      999
    );
    timestamps.push(date.getTime());
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return timestamps;
};
